package com.yuanren.em.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuanren.em.entity.MemberBenefitUsage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 会员权益使用记录Mapper接口
 */
public interface MemberBenefitUsageMapper extends BaseMapper<MemberBenefitUsage> {

    /**
     * 分页查询会员权益使用记录（带用户和权益信息）
     */
    @Select("SELECT bu.*, u.username, u.name as user_name, b.benefit_name as benefit_name, b.description as benefit_description " +
            "FROM t_member_benefit_usage bu " +
            "LEFT JOIN sys_user u ON bu.user_id = u.id " +
            "LEFT JOIN t_member_benefit b ON bu.benefit_id = b.id " +
            "WHERE (#{userId} IS NULL OR bu.user_id = #{userId}) " +
            "AND (#{usageType} IS NULL OR bu.usage_type = #{usageType}) " +
            "AND (#{status} IS NULL OR bu.status = #{status}) " +
            "ORDER BY bu.usage_date DESC")
    IPage<MemberBenefitUsage> selectUsagePageWithInfo(Page<MemberBenefitUsage> page, 
                                                      @Param("userId") Long userId,
                                                      @Param("usageType") String usageType,
                                                      @Param("status") String status);

    /**
     * 根据用户ID查询权益使用记录
     */
    @Select("SELECT bu.*, b.benefit_name as benefit_name, b.description as benefit_description " +
            "FROM t_member_benefit_usage bu " +
            "LEFT JOIN t_member_benefit b ON bu.benefit_id = b.id " +
            "WHERE bu.user_id = #{userId} " +
            "ORDER BY bu.usage_date DESC " +
            "LIMIT #{limit}")
    List<MemberBenefitUsage> selectByUserId(@Param("userId") Long userId, @Param("limit") Integer limit);

    /**
     * 统计用户权益使用情况
     */
    @Select("SELECT usage_type, COUNT(*) as count, " +
            "COALESCE(SUM(discount_amount), 0) as total_discount " +
            "FROM t_member_benefit_usage " +
            "WHERE user_id = #{userId} AND status = 'ACTIVE' " +
            "GROUP BY usage_type")
    List<Map<String, Object>> selectUsageStatsByUserId(@Param("userId") Long userId);

    /**
     * 统计权益使用总体情况
     */
    @Select("SELECT " +
            "COUNT(*) as total_usage, " +
            "COUNT(DISTINCT user_id) as unique_users, " +
            "COALESCE(SUM(discount_amount), 0) as total_discount, " +
            "COALESCE(AVG(discount_amount), 0) as avg_discount " +
            "FROM t_member_benefit_usage " +
            "WHERE status = 'ACTIVE' " +
            "AND DATE(usage_date) >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)")
    Map<String, Object> selectUsageStats();

    /**
     * 根据订单ID查询权益使用记录
     */
    @Select("SELECT bu.*, b.benefit_name as benefit_name " +
            "FROM t_member_benefit_usage bu " +
            "LEFT JOIN t_member_benefit b ON bu.benefit_id = b.id " +
            "WHERE bu.order_id = #{orderId}")
    List<MemberBenefitUsage> selectByOrderId(@Param("orderId") Long orderId);
} 